using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlTypes;
using System.Globalization;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using ns16;

namespace ns20
{
	internal static class Class182
	{
		internal struct Struct30 : IEquatable<Struct30>
		{
			private readonly Type type_0;

			private readonly Type type_1;

			public Type Type_0 => type_0;

			public Type Type_1 => type_1;

			public Struct30(Type initialType, Type targetType)
			{
				type_0 = initialType;
				type_1 = targetType;
			}

			public override int GetHashCode()
			{
				return type_0.GetHashCode() ^ type_1.GetHashCode();
			}

			public override bool Equals(object obj)
			{
				if (!(obj is Struct30))
				{
					return false;
				}
				return Equals((Struct30)obj);
			}

			public bool Equals(Struct30 other)
			{
				if (type_0 == other.type_0)
				{
					return type_1 == other.type_1;
				}
				return false;
			}
		}

		[CompilerGenerated]
		private sealed class Class183
		{
			public Delegate3<object, object> delegate3_0;

			public object method_0(object object_0)
			{
				return delegate3_0(null, object_0);
			}
		}

		private static readonly Dictionary<Type, Enum17> dictionary_0 = new Dictionary<Type, Enum17>
		{
			{
				typeof(char),
				Enum17.Char
			},
			{
				typeof(char?),
				Enum17.CharNullable
			},
			{
				typeof(bool),
				Enum17.Boolean
			},
			{
				typeof(bool?),
				Enum17.BooleanNullable
			},
			{
				typeof(sbyte),
				Enum17.SByte
			},
			{
				typeof(sbyte?),
				Enum17.SByteNullable
			},
			{
				typeof(short),
				Enum17.Int16
			},
			{
				typeof(short?),
				Enum17.Int16Nullable
			},
			{
				typeof(ushort),
				Enum17.UInt16
			},
			{
				typeof(ushort?),
				Enum17.UInt16Nullable
			},
			{
				typeof(int),
				Enum17.Int32
			},
			{
				typeof(int?),
				Enum17.Int32Nullable
			},
			{
				typeof(byte),
				Enum17.Byte
			},
			{
				typeof(byte?),
				Enum17.ByteNullable
			},
			{
				typeof(uint),
				Enum17.UInt32
			},
			{
				typeof(uint?),
				Enum17.UInt32Nullable
			},
			{
				typeof(long),
				Enum17.Int64
			},
			{
				typeof(long?),
				Enum17.Int64Nullable
			},
			{
				typeof(ulong),
				Enum17.UInt64
			},
			{
				typeof(ulong?),
				Enum17.UInt64Nullable
			},
			{
				typeof(float),
				Enum17.Single
			},
			{
				typeof(float?),
				Enum17.SingleNullable
			},
			{
				typeof(double),
				Enum17.Double
			},
			{
				typeof(double?),
				Enum17.DoubleNullable
			},
			{
				typeof(DateTime),
				Enum17.DateTime
			},
			{
				typeof(DateTime?),
				Enum17.DateTimeNullable
			},
			{
				typeof(DateTimeOffset),
				Enum17.DateTimeOffset
			},
			{
				typeof(DateTimeOffset?),
				Enum17.DateTimeOffsetNullable
			},
			{
				typeof(decimal),
				Enum17.Decimal
			},
			{
				typeof(decimal?),
				Enum17.DecimalNullable
			},
			{
				typeof(Guid),
				Enum17.Guid
			},
			{
				typeof(Guid?),
				Enum17.GuidNullable
			},
			{
				typeof(TimeSpan),
				Enum17.TimeSpan
			},
			{
				typeof(TimeSpan?),
				Enum17.TimeSpanNullable
			},
			{
				typeof(BigInteger),
				Enum17.BigInteger
			},
			{
				typeof(BigInteger?),
				Enum17.BigIntegerNullable
			},
			{
				typeof(Uri),
				Enum17.Uri
			},
			{
				typeof(string),
				Enum17.String
			},
			{
				typeof(byte[]),
				Enum17.Bytes
			},
			{
				typeof(DBNull),
				Enum17.DBNull
			}
		};

		private static readonly List<Class181> list_0 = new List<Class181>
		{
			new Class181
			{
				Type_0 = typeof(object),
				Enum17_0 = Enum17.Empty
			},
			new Class181
			{
				Type_0 = typeof(object),
				Enum17_0 = Enum17.Object
			},
			new Class181
			{
				Type_0 = typeof(object),
				Enum17_0 = Enum17.DBNull
			},
			new Class181
			{
				Type_0 = typeof(bool),
				Enum17_0 = Enum17.Boolean
			},
			new Class181
			{
				Type_0 = typeof(char),
				Enum17_0 = Enum17.Char
			},
			new Class181
			{
				Type_0 = typeof(sbyte),
				Enum17_0 = Enum17.SByte
			},
			new Class181
			{
				Type_0 = typeof(byte),
				Enum17_0 = Enum17.Byte
			},
			new Class181
			{
				Type_0 = typeof(short),
				Enum17_0 = Enum17.Int16
			},
			new Class181
			{
				Type_0 = typeof(ushort),
				Enum17_0 = Enum17.UInt16
			},
			new Class181
			{
				Type_0 = typeof(int),
				Enum17_0 = Enum17.Int32
			},
			new Class181
			{
				Type_0 = typeof(uint),
				Enum17_0 = Enum17.UInt32
			},
			new Class181
			{
				Type_0 = typeof(long),
				Enum17_0 = Enum17.Int64
			},
			new Class181
			{
				Type_0 = typeof(ulong),
				Enum17_0 = Enum17.UInt64
			},
			new Class181
			{
				Type_0 = typeof(float),
				Enum17_0 = Enum17.Single
			},
			new Class181
			{
				Type_0 = typeof(double),
				Enum17_0 = Enum17.Double
			},
			new Class181
			{
				Type_0 = typeof(decimal),
				Enum17_0 = Enum17.Decimal
			},
			new Class181
			{
				Type_0 = typeof(DateTime),
				Enum17_0 = Enum17.DateTime
			},
			new Class181
			{
				Type_0 = typeof(object),
				Enum17_0 = Enum17.Empty
			},
			new Class181
			{
				Type_0 = typeof(string),
				Enum17_0 = Enum17.String
			}
		};

		private static readonly Class179<Struct30, Func<object, object>> class179_0 = new Class179<Struct30, Func<object, object>>(smethod_5);

		public static Enum17 smethod_0(Type type_0)
		{
			if (dictionary_0.TryGetValue(type_0, out var value))
			{
				return value;
			}
			if (type_0.smethod_7())
			{
				return smethod_0(Enum.GetUnderlyingType(type_0));
			}
			if (Class195.smethod_10(type_0))
			{
				Type underlyingType = Nullable.GetUnderlyingType(type_0);
				if (underlyingType.smethod_7())
				{
					Type type_ = typeof(Nullable<>).MakeGenericType(Enum.GetUnderlyingType(underlyingType));
					return smethod_0(type_);
				}
			}
			return Enum17.Object;
		}

		public static Enum17 smethod_1(object object_0)
		{
			return smethod_0(object_0.GetType());
		}

		public static Class181 smethod_2(IConvertible iconvertible_0)
		{
			return list_0[(int)iconvertible_0.GetTypeCode()];
		}

		public static bool smethod_3(Type type_0)
		{
			return typeof(IConvertible).IsAssignableFrom(type_0);
		}

		public static TimeSpan smethod_4(string string_0)
		{
			return TimeSpan.Parse(string_0, CultureInfo.InvariantCulture);
		}

		private static Func<object, object> smethod_5(Struct30 struct30_0)
		{
			MethodInfo method = struct30_0.Type_1.GetMethod("op_Implicit", new Type[1] { struct30_0.Type_0 });
			if (method == null)
			{
				method = struct30_0.Type_1.GetMethod("op_Explicit", new Type[1] { struct30_0.Type_0 });
			}
			if (method == null)
			{
				return null;
			}
			Delegate3<object, object> delegate3_0 = Class140.Class167_0.ns20_002EClass167_002E_206A_206E_200E_202E_206E_200B_200B_200E_202C_206A_202A_202B_200C_206A_206C_206D_202B_200F_206E_200F_202C_200D_200D_202B_202B_206F_206D_206E_206D_206B_200B_206B_200B_200E_200D_202E_202A_206C_200F_202B_202E<object>(method);
			return (object object_0) => delegate3_0(null, object_0);
		}

		internal static BigInteger smethod_6(object object_0)
		{
			if (object_0 is BigInteger)
			{
				return (BigInteger)object_0;
			}
			if (object_0 is string)
			{
				return BigInteger.Parse((string)object_0, CultureInfo.InvariantCulture);
			}
			if (object_0 is float)
			{
				return new BigInteger((float)object_0);
			}
			if (object_0 is double)
			{
				return new BigInteger((double)object_0);
			}
			if (object_0 is decimal)
			{
				return new BigInteger((decimal)object_0);
			}
			if (object_0 is int)
			{
				return new BigInteger((int)object_0);
			}
			if (object_0 is long)
			{
				return new BigInteger((long)object_0);
			}
			if (object_0 is uint)
			{
				return new BigInteger((uint)object_0);
			}
			if (object_0 is ulong)
			{
				return new BigInteger((ulong)object_0);
			}
			if (!(object_0 is byte[]))
			{
				throw new InvalidCastException("Cannot convert {0} to BigInteger.".smethod_0(CultureInfo.InvariantCulture, object_0.GetType()));
			}
			return new BigInteger((byte[])object_0);
		}

		public static object smethod_7(BigInteger bigInteger_0, Type type_0)
		{
			if (type_0 == typeof(decimal))
			{
				return (decimal)bigInteger_0;
			}
			if (type_0 == typeof(double))
			{
				return (double)bigInteger_0;
			}
			if (type_0 == typeof(float))
			{
				return (float)bigInteger_0;
			}
			if (type_0 == typeof(ulong))
			{
				return (ulong)bigInteger_0;
			}
			try
			{
				return Convert.ChangeType((long)bigInteger_0, type_0, CultureInfo.InvariantCulture);
			}
			catch (Exception innerException)
			{
				throw new InvalidOperationException("Can not convert from BigInteger to {0}.".smethod_0(CultureInfo.InvariantCulture, type_0), innerException);
			}
		}

		public static object smethod_8(object object_0, CultureInfo cultureInfo_0, Type type_0)
		{
			if (object_0 == null)
			{
				throw new ArgumentNullException("initialValue");
			}
			if (Class195.smethod_10(type_0))
			{
				type_0 = Nullable.GetUnderlyingType(type_0);
			}
			Type type = object_0.GetType();
			if (type_0 == type)
			{
				return object_0;
			}
			if (smethod_3(object_0.GetType()) && smethod_3(type_0))
			{
				if (type_0.smethod_7())
				{
					if (object_0 is string)
					{
						return Enum.Parse(type_0, object_0.ToString(), ignoreCase: true);
					}
					if (smethod_14(object_0))
					{
						return Enum.ToObject(type_0, object_0);
					}
				}
				return Convert.ChangeType(object_0, type_0, cultureInfo_0);
			}
			if (object_0 is DateTime && type_0 == typeof(DateTimeOffset))
			{
				return new DateTimeOffset((DateTime)object_0);
			}
			if (object_0 is byte[] && type_0 == typeof(Guid))
			{
				return new Guid((byte[])object_0);
			}
			if (object_0 is string)
			{
				if (type_0 == typeof(Guid))
				{
					return new Guid((string)object_0);
				}
				if (type_0 == typeof(Uri))
				{
					return new Uri((string)object_0, UriKind.RelativeOrAbsolute);
				}
				if (type_0 == typeof(TimeSpan))
				{
					return smethod_4((string)object_0);
				}
				if (typeof(Type).IsAssignableFrom(type_0))
				{
					return Type.GetType((string)object_0, throwOnError: true);
				}
			}
			if (type_0 == typeof(BigInteger))
			{
				return smethod_6(object_0);
			}
			if (object_0 is BigInteger)
			{
				return smethod_7((BigInteger)object_0, type_0);
			}
			TypeConverter typeConverter = smethod_13(type);
			if (typeConverter != null && typeConverter.CanConvertTo(type_0))
			{
				return typeConverter.ConvertTo(null, cultureInfo_0, object_0, type_0);
			}
			TypeConverter typeConverter2 = smethod_13(type_0);
			if (typeConverter2 != null && typeConverter2.CanConvertFrom(type))
			{
				return typeConverter2.ConvertFrom(null, cultureInfo_0, object_0);
			}
			if (object_0 == DBNull.Value)
			{
				if (Class195.smethod_9(type_0))
				{
					return smethod_11(null, type, type_0);
				}
				throw new Exception("Can not convert null {0} into non-nullable {1}.".smethod_1(CultureInfo.InvariantCulture, type, type_0));
			}
			if (!(object_0 is INullable))
			{
				if (!type_0.smethod_3() && !type_0.smethod_5() && !type_0.smethod_10())
				{
					throw new InvalidOperationException("Can not convert from {0} to {1}.".smethod_1(CultureInfo.InvariantCulture, type, type_0));
				}
				throw new ArgumentException("Target type {0} is not a value type or a non-abstract class.".smethod_0(CultureInfo.InvariantCulture, type_0), "targetType");
			}
			return smethod_11(smethod_12((INullable)object_0), type, type_0);
		}

		public static bool smethod_9(object object_0, CultureInfo cultureInfo_0, Type type_0, out object object_1)
		{
			try
			{
				object_1 = smethod_8(object_0, cultureInfo_0, type_0);
				return true;
			}
			catch
			{
				object_1 = null;
				return false;
			}
		}

		public static object smethod_10(object object_0, CultureInfo cultureInfo_0, Type type_0)
		{
			if (type_0 == typeof(object))
			{
				return object_0;
			}
			if (object_0 == null && Class195.smethod_9(type_0))
			{
				return null;
			}
			if (smethod_9(object_0, cultureInfo_0, type_0, out var object_))
			{
				return object_;
			}
			return smethod_11(object_0, Class195.smethod_3(object_0), type_0);
		}

		private static object smethod_11(object object_0, Type type_0, Type type_1)
		{
			Type type = object_0?.GetType();
			if (object_0 != null)
			{
				if (type_1.IsAssignableFrom(type))
				{
					return object_0;
				}
				Func<object, object> func = class179_0.method_0(new Struct30(type, type_1));
				if (func != null)
				{
					return func(object_0);
				}
			}
			else if (Class195.smethod_9(type_1))
			{
				return null;
			}
			throw new ArgumentException("Could not cast or convert from {0} to {1}.".smethod_1(CultureInfo.InvariantCulture, (type_0 != null) ? type_0.ToString() : "{null}", type_1));
		}

		public static object smethod_12(INullable inullable_0)
		{
			if (inullable_0 == null)
			{
				return null;
			}
			if (inullable_0 is SqlInt32)
			{
				return smethod_12((SqlInt32)(object)inullable_0);
			}
			if (inullable_0 is SqlInt64)
			{
				return smethod_12((SqlInt64)(object)inullable_0);
			}
			if (inullable_0 is SqlBoolean)
			{
				return smethod_12((SqlBoolean)(object)inullable_0);
			}
			if (inullable_0 is SqlString)
			{
				return smethod_12((SqlString)(object)inullable_0);
			}
			if (!(inullable_0 is SqlDateTime))
			{
				throw new ArgumentException("Unsupported INullable type: {0}".smethod_0(CultureInfo.InvariantCulture, inullable_0.GetType()));
			}
			return smethod_12((SqlDateTime)(object)inullable_0);
		}

		internal static TypeConverter smethod_13(Type type_0)
		{
			return Class140.smethod_11(type_0);
		}

		public static bool smethod_14(object object_0)
		{
			switch (smethod_1(object_0))
			{
			default:
				return false;
			case Enum17.SByte:
			case Enum17.Int16:
			case Enum17.UInt16:
			case Enum17.Int32:
			case Enum17.Byte:
			case Enum17.UInt32:
			case Enum17.Int64:
			case Enum17.UInt64:
				return true;
			}
		}

		public static int smethod_15(char[] char_0, int int_0, int int_1)
		{
			if (int_1 == 0)
			{
				throw new FormatException("Input string was not in a correct format.");
			}
			bool flag;
			if (flag = char_0[int_0] == '-')
			{
				if (int_1 == 1)
				{
					throw new FormatException("Input string was not in a correct format.");
				}
				int_0++;
				int_1--;
			}
			int num = 0;
			int num2 = int_0 + int_1;
			int num3 = int_0;
			while (true)
			{
				if (num3 < num2)
				{
					int num4 = char_0[num3] - 48;
					if (num4 >= 0 && num4 <= 9)
					{
						int num5 = 10 * num - num4;
						if (num5 <= num)
						{
							num = num5;
							num3++;
							continue;
						}
						throw new OverflowException();
					}
					throw new FormatException("Input string was not in a correct format.");
				}
				if (flag)
				{
					break;
				}
				if (num == int.MinValue)
				{
					throw new OverflowException();
				}
				num = -num;
				break;
			}
			return num;
		}

		public static Enum18 smethod_16(char[] char_0, int int_0, int int_1, out long long_0)
		{
			long_0 = 0L;
			if (int_1 == 0)
			{
				return Enum18.Invalid;
			}
			bool flag;
			if (flag = char_0[int_0] == '-')
			{
				if (int_1 == 1)
				{
					return Enum18.Invalid;
				}
				int_0++;
				int_1--;
			}
			int num = int_0 + int_1;
			int num2 = int_0;
			while (true)
			{
				if (num2 < num)
				{
					int num3 = char_0[num2] - 48;
					if (num3 < 0 || num3 > 9)
					{
						break;
					}
					long num4 = 10L * long_0 - num3;
					if (num4 <= long_0)
					{
						long_0 = num4;
						num2++;
						continue;
					}
					return Enum18.Overflow;
				}
				if (!flag)
				{
					if (long_0 == long.MinValue)
					{
						return Enum18.Overflow;
					}
					long_0 = -long_0;
				}
				return Enum18.Success;
			}
			return Enum18.Invalid;
		}
	}
}
